package com.ft.oa.web.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.dto.condition.SysUserDTO;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.service.sys.SysUserService;
import com.ft.oa.utils.OperateLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public ApiResult userInfo(){

        SysUser user = sysUserService.info();

        return ApiResult.successWithData(user);
    }


    /**
     * 新增用户
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult addUser(@RequestBody SysUserDTO user){

        ApiResult result = sysUserService.add(user);
        //记录操作日志
        OperateLogUtils.saveOperateLog("创建新用户","系统管理", "一级");
        return result;
    }

    /**
     * 修改用户
     * @return
     */
    @PostMapping("/edit")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult edit(@RequestBody SysUserDTO user){
        if(user.getId() == 1){
            return ApiResult.builder().code(10003).msg("超级管理员不能修改").build();
        }
        ApiResult result = sysUserService.edit(user);
        return result;
    }

    /**
     * 删除用户
     * @return
     */
    @PostMapping("/del/{id}")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult del(@PathVariable Long id){
        if(id == 1){
            return ApiResult.builder().code(10002).msg("超级管理员不能删除").build();
        }
        sysUserService.del(id);
        return ApiResult.simpleSuccess();
    }

    /**
     * 用户分页
     * @return
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult page(@RequestBody SysUserDTO user){

        Page<SysUser> result = sysUserService.page(user, user.getPage(), user.getSize());
        return ApiResult.successWithData(result);
    }


    /**
     * 所有可用用户
     * @return
     */
    @GetMapping("/list/all")
    public ApiResult listAll(){
        List<SysUser> list = sysUserService.listAll();
        return ApiResult.successWithData(list);
    }




}
